/**
 * 外部依赖jquery
 */

import layer from "./module/base/layer.js";
import request from "./module/base/request.js";
import permission from "./module/base/permission.js";
import form from "./module/form.js";
import menu from "./module/menu.js";
import page from "./module/page.js";
import bread from "./module/bread.js";
import button from "./module/button.js";
import table from "./module/table.js";

/**
 * 自定义框架js
 */

/**
 * 所有模块
 */
var module = {
	layer,
	request,
	permission,
	form,
	menu,
	page,
	bread,
	button,
	table,
}

/**
 * 渲染选项
 */
var option = {
	menuData: "permission/listUserMenu",
	menuElem: '#menu', //菜单元素
	pageElem: '#page', //加载内容元素
}

var logout = function() { } //退出方法
var body = $("body")
var admin = new function() {

	/**
	 * 获取模块
	 */
	this.getModule = function(name) {
		return module[name];
	}

	/**
	 * 渲染框架
	 */
	this.render = function(opt) {
		$.extend(option, opt)
		admin.listener();
		page.render({ elem: option.pageElem })
		bread.render({ elem: option.breadElem })
		menu.render({ elem: option.menuElem })
		menu.click(admin.startPage);//加载内容
		bread.click(admin.toPage)
		if (typeof option.menuData === "string") {
			request.post(option.menuData).then(res => {
				var treeData = packMenuTree(res.data)
				menu.loadMenu(treeData); //加载菜单
				var firstMenu = getFirstMenu(treeData)
				admin.startPage(firstMenu.url, firstMenu.title)
			})
		} else {
			menu.loadMenu(option.menuData); //加载菜单
			var firstMenu = getFirstMenu(option.menuData)
			admin.startPage(firstMenu.url, firstMenu.title)
		}

	}


	this.logout = function(callback) {
		logout = callback
	}

	this.startPage = function(url, title) {
		var detail = menu.activeMenu(url);
		var cata = detail == null ? null : [{ url: detail.purl, title: detail.ptitle }]
		bread.start({ url: url, title: title }, cata);
		page.loadPage({ url: url });
	}

	this.toPage = function(url, title) {
		menu.activeMenu(url);
		bread.change({ url: url, title: title });
		page.loadPage({ url: url });
	}

	/** 
	 * 渲染选项操作
	*/
	this.option = function(key, value) {
		if (value) {
			option[key] = value;
		}
		return option[key];
	}
	/**
	 * 菜单元素操作
	 */
	this.menuElem = function(value) {
		return admin.option('menuElem', value);
	}

	/**
	 * 内容元素操作
	 */
	this.pageElem = function(value) {
		return admin.option('pageElem', value);
	}

	this.listener = function() {
		body.on("click", ".logout", function() {
			logout()
		})

		// 侧边栏
		$("body").on('click', '.lyear-aside-toggler', function() {
			$('.lyear-layout-sidebar').toggleClass('lyear-aside-open');
			$("body").toggleClass('lyear-layout-sidebar-close');

			$('.lyear-mask-modal').remove();
		});

		$("body").on('click','[data-toggle="startPage"]', function() {
			var url = $(this).data('url');
			var title = $(this).data('title');
			admin.startPage(url, title);
		})

		$("body").on('click', '[data-toggle="page"]', function() {
			var url = $(this).data('url');
			var title = $(this).data('title');
			admin.toPage(url, title);
		})
	}


}

/**
		 * 获取第一个菜单
		 */
function getFirstMenu(treeMenu) {
	if (treeMenu.length == 0) {
		return null;
	}
	var firstCata = treeMenu[0];
	if (firstCata.type == '01') {
		return firstCata.children[0]
	}
	if (firstCata.type == '02') {
		return firstCata;
	}
}

/**
 * 将菜单改为树形结构
 */
function packMenuTree(data) {
	var treeMenu = [];
	var menu = [];
	data.forEach(item => {
		if (item.type == '01') {
			treeMenu.push(item);
		} else if (item.type == '02') {
			menu.push(item);
		}
	})
	menu.forEach(item => {
		treeMenu.forEach(item2 => {
			if (item.pid == item2.id) {
				if (item2.children == undefined) {
					item2.children = []
				}
				item2.children.push(item);
			}
		})
	})
	return treeMenu;
}

window.admin = admin;
export default admin;
